package com.ruoyi.webs.utils.weixinlogin;

import com.alibaba.fastjson.JSONObject;


public class WeiXinUtil {
    public static final String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    public static final String oauth2_1_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
    public static final String oauth2_2_url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
    public static final String get_userInfo_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
    public static final String get_hangye_url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";
    public static final String appid = "wx6d3bdf8f76b1efd6";
    public static final String appSecret = "4f7853e5163ff2456b2b8634b1fd1f91";

    public static AccessToken getAccessToken(String appid, String appSecret) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET".replace("APPID", appid).replace("APPSECRET", appSecret);
        AccessToken accesstoken = new AccessToken();

        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);


        accesstoken.setToken(jsonObject.getString("access_token"));

        accesstoken.setExpiresIn(jsonObject.getIntValue("expires_in"));
        return accesstoken;
    }


    public static WeixinOauth2Token getOauth2AccessToken(String appId, String appSecret, String code) {
        String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code".replace("APPID", appId).replace("SECRET", appSecret).replace("CODE", code);

        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, EnumMethod.GET.name(), null);
        WeixinOauth2Token wxo = new WeixinOauth2Token();
        wxo.setAccessToken(jsonObject.getString("access_token"));
        wxo.setExpiresIn(jsonObject.getIntValue("expires_in"));
        wxo.setRefreshToken(jsonObject.getString("refresh_token"));
        wxo.setOpenId(jsonObject.getString("openid"));
        wxo.setScope(jsonObject.getString("scope"));

        return wxo;
    }


    public static SNSUserInfo getSNSUserInfo(String accessToken, String openId) {
        String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN".replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
        SNSUserInfo snsuserinfo = new SNSUserInfo();

        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, EnumMethod.GET.name(), null);

        snsuserinfo.setOpenId(jsonObject.getString("openid"));
        snsuserinfo.setNickname(jsonObject.getString("nickname"));
        snsuserinfo.setSex(jsonObject.getIntValue("sex"));
        snsuserinfo.setCountry(jsonObject.getString("country"));
        snsuserinfo.setProvince(jsonObject.getString("province"));
        snsuserinfo.setCity(jsonObject.getString("city"));
        snsuserinfo.setHeadImgUrl(jsonObject.getString("headimgurl"));

        return snsuserinfo;
    }


    public static String createUrl(String redirectUri) {
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect".replace("APPID", "wx6d3bdf8f76b1efd6").replace("REDIRECT_URI", CommonUtil.urlEncodeUTF8(redirectUri)).replace("SCOPE", "snsapi_userinfo");
        System.out.println(url);
        return url;
    }


    public static String shortURL(String longURL, String wxAppId, String secret) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN";

        try {
            requestUrl = requestUrl.replace("ACCESS_TOKEN", getAccessToken(wxAppId, secret).getToken());
        } catch (Exception e) {

            e.printStackTrace();
        }
        String jsonMsg = "{\"action\":\"long2short\",\"long_url\":\"%s\"}";

        String.format(jsonMsg, new Object[]{longURL});
        JSONObject jsonobject = CommonUtil.httpsRequest(requestUrl, "POST", String.format(jsonMsg, new Object[]{longURL}));

        return jsonobject.getString("short_url");
    }


    public static void main(String[] args) {
        AccessToken a = getAccessToken("wx6d3bdf8f76b1efd6", "4f7853e5163ff2456b2b8634b1fd1f91");


        System.out.println(a.getToken());
        System.out.println(a.getExpiresIn());
    }
}